<?php
function GetContactInfos(){
	$conn = GetConnection();
	return $conn->query('SELECT * FROM Contact_Info');
}

function GetContactInfos_Sorted(){
	$conn = GetConnection();
	return $conn->query('SELECT * FROM Contact_Info ORDER BY id DESC');
}

function GetContactInfo($id){
	$conn = GetConnection();
	if($id == null)
		die("No Contact could be found. I.D. invalid.");
	$results = $conn->query("SELECT * FROM Contact_Info WHERE id='$id'");
	$row = $results->fetch_assoc();
	$conn->close();
	return $row;
}

function EditContactInfo($row){
	$conn = GetConnection();
	$id = $row['id'];
	$customer_id = $row['customer_id'];
	$value = strip_tags($row['value']);
	$contact_keyword = $row['contact_keyword'];
	
	if(strlen($value) > 0)
	{
		$conn->query("UPDATE Contact_Info SET value='$value', Customers_id = '$customer_id', Contact_Keyword_id = '$contact_keyword' WHERE id='$id'") or die(mysql_error());
	}
	else
		header("Location: http://cs.newpaltz.edu/~N02432643/%20web-course-2012/final/ContactInfos/edit.php?id=".$id."");
	
	$conn->close();
}
function NewContactInfo($row){
	$customer_id = $row['customer_id'];
	$value = strip_tags($row['value']);
	$contact_keyword = $row['contact_keyword'];
	if(strlen($value) > 0)
	{
		$conn = GetConnection();
		if($conn->query("INSERT INTO Contact_Info (id,Customers_id,value,Contact_Keyword_id) VALUES ('','$customer_id','$value','$contact_keyword')")){
			$conn->close();
		}
		else{
			die("Query failed");
		}
	}
	else {
			die("You need to enter a Contact Info name.");
	}
}

function DeleteContactInfo($id){
		$conn = GetConnection();
		$results = $conn->query("Delete FROM Contact_Info WHERE id=$id");
		header('Location: http://cs.newpaltz.edu/~N02432643/%20web-course-2012/final/ContactInfo/');
}

function GetContactKeywords(){
	$conn = GetConnection();
	return $conn->query('SELECT * FROM Contact_Keyword');
}

function GetCustomers(){
	$conn = GetConnection();
	return $conn->query('SELECT * FROM Customers');
}